package com.trs.crypt;

import java.io.*;
import java.util.UUID;

/**
 * @ClassName KeyJNILoader
 * @Description 描述
 * @Detail detail
 * @Author MyPC
 * @Date 2020/12/11
 * @Version 1.0
 */
public class KeyJNILoader {
    public KeyJNILoader() {
    }

    public static boolean hasResource(String path) {
        return KeyJNILoader.class.getResource(path) != null;
    }

    private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
        if (!(in1 instanceof BufferedInputStream)) {
            in1 = new BufferedInputStream((InputStream)in1);
        }

        if (!(in2 instanceof BufferedInputStream)) {
            in2 = new BufferedInputStream((InputStream)in2);
        }

        int ch2;
        for(int ch = ((InputStream)in1).read(); ch != -1; ch = ((InputStream)in1).read()) {
            ch2 = ((InputStream)in2).read();
            if (ch != ch2) {
                return false;
            }
        }

        ch2 = ((InputStream)in2).read();
        return ch2 == -1;
    }

    public static File extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
        String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
        String uuid = UUID.randomUUID().toString();
        String extractedLibFileName = uuid + libraryFileName;
        File extractedLibFile = new File(targetFolder, extractedLibFileName);

        try {
            InputStream reader = KeyJNILoader.class.getResourceAsStream(nativeLibraryFilePath);
            FileOutputStream writer = new FileOutputStream(extractedLibFile);

            try {
                byte[] buffer = new byte[8192];
                boolean var10 = false;

                int bytesRead;
                while((bytesRead = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, bytesRead);
                }
            } finally {
                extractedLibFile.deleteOnExit();
                if (writer != null) {
                    writer.close();
                }

                if (reader != null) {
                    reader.close();
                }

            }

            extractedLibFile.setReadable(true);
            extractedLibFile.setWritable(true, true);
            extractedLibFile.setExecutable(true);
            InputStream nativeIn = KeyJNILoader.class.getResourceAsStream(nativeLibraryFilePath);
            FileInputStream extractedLibIn = new FileInputStream(extractedLibFile);

            try {
                if (!contentsEquals(nativeIn, extractedLibIn)) {
                    throw new RuntimeException("Failed to write a native library file at %s" + extractedLibFile);
                }
            } finally {
                if (nativeIn != null) {
                    nativeIn.close();
                }

                if (extractedLibIn != null) {
                    extractedLibIn.close();
                }

            }

            return new File(targetFolder, extractedLibFileName);
        } catch (IOException var21) {
            var21.printStackTrace(System.err);
            return null;
        }
    }
}
